package domain;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;


@Entity
@Table(name = "confirmaciones")
public class Confirmation extends PersistentEntity{
	
	public enum Calification{
		negative("Negative"), neutral("Neutral"), positive("Positive");
		String value;
		Calification(String value){
			this.value = value;
		}
		@Override
		public String toString() {
			return value;
		}
	}
	
	public enum Confirmated{
		realize("Realized"), notyetconfirm("Not yet confirmed"), dontrealize("Not realized");
		String value;
		Confirmated(String value){
			this.value = value;
		}
		@Override
		public String toString() {
			return value;
		}
		
	}
	
	private String comment;
	@Enumerated(EnumType.STRING)
	private Calification calification;
	@Enumerated(EnumType.STRING)
	private Confirmated sellerConfirmation;
	@Enumerated(EnumType.STRING)
	private Confirmated buyerConfirmation;
	
	//Hibernate
	public Confirmation(){
		calification = Calification.neutral;
		sellerConfirmation = Confirmated.notyetconfirm;
		buyerConfirmation = Confirmated.notyetconfirm;
	}
	
	public Confirmation(String comment, 
			Calification calification, Confirmated sellerConfirmation, Confirmated buyerConfirmation){
		this.comment = comment;
		this.calification = calification;
		this.sellerConfirmation = sellerConfirmation;
		this.buyerConfirmation = buyerConfirmation;
	}
	
	public boolean isConfirmed(){
		if(sellerConfirmation == Confirmated.realize && buyerConfirmation == Confirmated.realize){
			return true;
		}

		if(sellerConfirmation == Confirmated.dontrealize && buyerConfirmation == Confirmated.dontrealize)
			return true;
		return false;
	}
	
	public boolean isBuyerConfirmed(){
		if(buyerConfirmation == Confirmated.notyetconfirm){
			return false;
		}
		return true;
	}
	
	public boolean hasConflict(){
		if(sellerConfirmation == Confirmated.dontrealize && buyerConfirmation == Confirmated.realize)
			return true;
		if(sellerConfirmation == Confirmated.realize && buyerConfirmation == Confirmated.dontrealize)
			return true;
		return false;
	}
	
	public boolean hasFinish(){
		if (hasConflict() || succesful() || fail()) {
			return true;
		}
		
		return false;
	}

	public boolean succesful() {
		return (sellerConfirmation == Confirmated.realize && buyerConfirmation == Confirmated.realize);
	}
	
	public boolean fail() {
		return (sellerConfirmation == Confirmated.dontrealize && buyerConfirmation == Confirmated.dontrealize);
	}
	
	public String getComment() {
		return comment;
	}

	public Calification getPoints() {
		return calification;
	}

	public Confirmated getSellerConfirmation() {
		return sellerConfirmation;
	}

	public void setSellerConfirmation(Confirmated sellerConfirmation) {
		this.sellerConfirmation = sellerConfirmation;
	}

	public Confirmated getBuyerConfirmation() {
		return buyerConfirmation;
	}

	public void setBuyerConfirmation(Confirmated buyerConfirmation) {
		this.buyerConfirmation = buyerConfirmation;
	}

	public void setCalification(Calification calification) {
		this.calification = calification;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	
	public boolean notConfirmed(){
		return buyerConfirmation==Confirmated.notyetconfirm || sellerConfirmation==Confirmated.notyetconfirm;
	}

}
